/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package geometry;

import java.util.Arrays;

/**
 *
 * @author rusak
 */
public class SimpleIntersectionAlgorithm implements IntersectionAlgorithm {

    @Override
    public Integer noOfIntersections(Segment[] efector, Obstacle[] obstacles) {
        int count = 0;
        for (int i = 0; i < efector.length; i++) {
            for (int j = 0; j < obstacles.length; j++) {
                for (int k = 0; k < obstacles[j].size(); k++) {
                    if (efector[i].intersect(obstacles[j].getEdgeAt(k))) {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    @Override
    public boolean checkIfIntersect(Segment[] efector, Obstacle[] obstacles) {
        for (int i = 0; i < efector.length; i++) {
            for (int j = 0; j < obstacles.length; j++) {
                for (int k = 0; k < obstacles[j].size(); k++) {
                    if (efector[i].intersect(obstacles[j].getEdgeAt(k))) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public Integer[] getIntersectedObstacles(Segment[] efector, Obstacle[] obstacles) {
        Integer result[] = new Integer[obstacles.length];
        int len = 0;
        int howManyTimes = 0;
        for (int j = 0; j < obstacles.length; j++) {
            howManyTimes = 0;
              for (int i = 0; i < efector.length; i++) {
            
                
                for (int k = 0; k < obstacles[j].size(); k++) {
                    
                    if (efector[i].intersect(obstacles[j].getEdgeAt(k))) {
                        howManyTimes++;
                        break;
                    }
                }
               
                if(howManyTimes > 0) {
                    result[len] = j;
                        len++;
                        break;
                }
            }
             //  System.out.println("OBSTACLE NR: "+j+" CROSSED "+howManyTimes+" TIMES");
        }
        return len > 0 ? Arrays.copyOfRange(result, 0, len) : new Integer[0];
    }
}
